﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaiwuZD
{

    public class TwItemTemplate : TwObject
    {

        /// <summary>
        /// 物品分类-大类
        /// </summary>
        public static TwItemType[] ItemTypes { get; } = { TwItemType.All, TwItemType.Manufacture, TwItemType.Medicine, TwItemType.Food, TwItemType.Equipment, TwItemType.Book, TwItemType.Other };

        public static TwItemType[] AddItemTypes { get; } = { TwItemType.Manufacture, TwItemType.Medicine, TwItemType.Food, TwItemType.Equipment, TwItemType.Book, TwItemType.Other };



        /// <summary>
        /// 分类
        /// </summary> 
        public TwItemType ItemType3 { get { return ValueAt<TwItemType>(4); } }

        public TwItemType2 ItemType { get { return new TwItemType2(ValueAt<int>(4), ValueAt<int>(5), ValueAt<int>(98)); } }


        /// <summary>
        /// 耐久度
        /// </summary>
        public int MaxDurability
        {
            get { return ValueAt<int>(902); }
            set { DataShop["902"] = value.ToString(); }
        }

        //private int DurabilityKey
        //{
        //    get
        //    {
        //        int key = 901;
        //        //switch (ItemType3)
        //        //{
        //        //    case TwItemType.Manufacture:
        //        //        key = 103;
        //        //        break;
        //        //    case TwItemType.Medicine:
        //        //        key = 103;
        //        //        break;
        //        //    case TwItemType.Food:
        //        //        key = 103;
        //        //        break;
        //        //    case TwItemType.Equipment:
        //        //        key = 901;
        //        //        break;
        //        //    case TwItemType.Book:
        //        //        key = 104;
        //        //        break;
        //        //}
        //        return key;
        //    }
        //}

        public int Durability
        {
            get { return ValueAt<int>(901); }
            set { DataShop[901] = value.ToString(); }
        }


        /// <summary>
        /// 
        /// </summary>
        public int CategoryAId { get { return ValueAt<int>(98); } }

        public int CategoryBId { get { return ValueAt<int>(44); } }


        public static Dictionary<int, string> CategoryBSet = new Dictionary<int, string>();



        //品级
        public int Level { get { return ValueAt<int>(8); } }


        public Dictionary<int, string> GetSaveInfo()
        {
            var dic = new Dictionary<int, string>();
            foreach (var item in DataShop)
            {
                dic.Add(int.Parse(item.Key), item.Value);
            }
            return dic;
        }



        public static TwItemTemplate LoadFromJson(JProperty jp)
        {
            TwItemTemplate ti = TwObject.LoadFromJson<TwItemTemplate>(jp) as TwItemTemplate;


            return ti;
        }


        public override object Clone()
        {
            var rr = new TwItem() { Name = this.Name, Id = this.Id, Description = this.Description, Amount = 1 };
            //rr.DataShop.Add("999", DataShop[999]);
            rr.DataShop.Add("4", DataShop[4]);
            rr.DataShop.Add("5", DataShop[5]);
            rr.DataShop.Add("98", DataShop[98]);
            return rr;
        }

        protected override string GetDefaultValue(string key)
        {
            if (GameAssets.TwItems != null)
            {
                if (GameAssets.TwItems.ContainsKey(Id))
                    if (GameAssets.TwItems[Id].DataShop.ContainsKey(key))
                        return GameAssets.TwItems[Id].DataShop[key];
            }

            return base.GetDefaultValue(key);
        }
    }

    public class TwItem : TwItemTemplate
    {
        protected override string[] RequiredFileds => new string[] { "0", "999", "4", "5", "98" };

        private int _key;
        /// <summary>
        /// 在背包中的键
        /// </summary>
        public int Key
        {
            get { return _key == 0 ? Id : _key; }
            set { _key = value; }
        }

        /// <summary>
        /// 数量
        /// </summary>
        public int Amount { get; set; }

        public int CurrentDurability
        {
            get { return Convert.ToInt32(this[901]); }
            set { DataShop["901"] = value.ToString(); }
        }

        public static TwItem Create(int key, int amount)
        {
            if (GameAssets.TwItems.ContainsKey(key))
            {
                var item = GameAssets.TwItems[key];

                if (item == null)
                    return null;

                var r = item.Clone() as TwItem;
                r.Amount = amount;
                r.SetData(901, item[902]);
                r.SetData(902, item[902]);

                //秘籍
                if (r.ItemType[0] == 5 && r.ItemType[1] == 21)
                {
                    int[] sd = new int[10];
                    //if (bBookFull)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            sd[i] = 1;
                        }
                    }
                    //else
                    //{
                    //    var rand = new Random();
                    //    for (int i = 0; i < 10; i++)
                    //    {
                    //        sd[i] = rand.Next(1);
                    //    }
                    //}
                    r.SetData(33, string.Join("|", sd));
                }

                return r;
            }
            else
                return null;
        }

        public static TwItem Create(Dictionary<int, string> savedata)
        {
            var itemid = int.Parse(savedata[999]);


            if (GameAssets.TwItems.ContainsKey(itemid))
            {
                var item = GameAssets.TwItems[itemid];


                var newitem = new TwItem { Name = item.Name, Id = item.Id, Description = item.Description };
                //newitem.DataShop = new TwDictionary(savedata);
                newitem.SetData(savedata);
                newitem.SetData(8, item.Level);
                newitem.SetData(4, item[4]);
                newitem.SetData(5, item[5]);
                newitem.SetData(98, item[98]);
                return newitem;
            }

            else
                return null;
        }

        public void SetBookPages(int[] pages)
        {
            if (ValueAt<int>(4) != 5 || ValueAt<int>(5) != 21)
                return;

            if (pages != null && pages.Length != 10)
                return;

            if (pages == null)
                DataShop[33] = "1|1|1|1|1|1|1|1|1|1";
            else
                DataShop[33] = string.Join("|", pages);
        }

        public TwItem() { }

        public TwItem(int id)
        {
            Id = id;

            foreach (var item in RequiredFileds)
            {
                DataShop[item] = GetDefaultValue(item);
            }
            if (ValueAt<int>(6) == 0)
                DataShop[901] = GetDefaultValue("902");
        }
    }

}
